
local rock_def_prototype =
{
	is_ground_content = true,
	sounds = generic_media.node_sound_stone_defaults(),
	groups = {cracky = 3, stone = 1},
	drop = "cobble",
}
local rubble_def_prototype =
{
	is_ground_content = false,
	groups = {crumbly = 3, falling_node = 1},
	sounds = generic_media.node_sound_gravel_defaults(),
	drop = "cobble",
}

local function get_rock_definition(texture)
	local ret = {}
	ret.tiles = {texture}
	for k, v in pairs(rock_def_prototype)
	do
		ret[k] = v
	end
	return ret
end
local function get_rubble_definition(texture)
	local ret = {}
	ret.tiles = {texture}
	for k, v in pairs(rubble_def_prototype)
	do
		ret[k] = v
	end
	return ret
end


local minerals =
{
	"coal",
	"iron",
	"gold",
	"diamond",


	get_ore_def_gold = function(parent_rock_name)
		return
		{
			ore_type = "scatter",
			ore = parent_rock_name .. "_with_gold",
			wherein = parent_rock_name .. "_with_iron",
			clust_scarcity = 8 * 8 * 8,
			clust_num_ores = 6,
			clust_size = 3,
			y_min = -30,
			y_max = 1000,
		}
	end,
	get_ore_def_iron = function(parent_rock_name)
		return
		{
			ore_type = "scatter",
			ore = parent_rock_name .. "_with_iron",
			wherein = parent_rock_name .. "_with_coal",
			clust_scarcity = 4 * 4 * 4,
			clust_num_ores = 9,
			clust_size = 3,
			y_min = -30,
			y_max = 1000,
		}
	end,
	get_ore_def_diamond = function(parent_rock_name)
		return
		{
			ore_type = "scatter",
			ore = parent_rock_name .. "_with_diamond",
			wherein = parent_rock_name .. "_with_iron",
			clust_scarcity = 8 * 8 * 8,
			clust_num_ores = 6,
			clust_size = 3,
			y_min = -30,
			y_max = 1000,
		}
	end,
	get_ore_def_coal = function(parent_rock_name)
		return
		{
			ore_type = "vein",
			ore = parent_rock_name .. "_with_coal",
			wherein = parent_rock_name,
			clust_scarcity = 16 * 16 * 16,
			clust_num_ores = 3,
			clust_size = 3,
			y_min = -30,
			y_max = 1000,
		}
	end,
}
--TODO: register rubble versions
local function register_ore_nodes(name,  rock_texture, rubble_texture, S)
	for _, mineral in ipairs(minerals)
	do
		local rockdef = get_rock_definition(rock_texture ..
			"^rocks_api_" .. mineral .. "_ore.png")
		local rubbledef = get_rubble_definition(rubble_texture ..
			"^rocks_api_" .. mineral .. "_rubble.png")

		local descr = string.capitalize_first(mineral) .. " Ore"
		rockdef.description = S(descr)
		rubbledef.description = S(descr .. " Rubble")

		rockdef.drop =
		{
			max_items = 3,
			items =
			{
				{rarity = 1, items = {"cobble"}},
				{rarity = 1, items = {mineral}},
				{rarity = 2, items = {mineral}},
			}
		}
		rubbledef.drop = rockdef.drop


		minetest.register_node(name .. "_with_" .. mineral, rockdef)
		minetest.register_ore(minerals["get_ore_def_" .. mineral](name))

		minetest.register_node(name .. "_rubble_with_" .. mineral, rubbledef)
		erosion.register_erosion({name .. "_with_" .. mineral,
			name .. "_rubble_with_" .. mineral})
	end
end




function rocks_api.register_rock(name, description, texture, rubble_texture, translation_textdomain)
	local S = minetest.get_translator(translation_textdomain)
	local rockdef = get_rock_definition(texture)
	local rubbledef = get_rubble_definition(rubble_texture)

	rockdef.description = S(description)
	rubbledef.description = S(description .. " Rubble")
	minetest.register_node(name, rockdef)
	minetest.register_node(name .. "_rubble", rubbledef)
	register_ore_nodes(name, texture, rubble_texture, S)
end
